import type {ResponsePaginationData} from '@/utils/request';
import {SUCCESS_CODE} from '@/utils/request';
import type {MoteInfoItem} from './data';
import {DeviceStatusParser} from '../../../utils/utils';
import {request} from 'umi';
import type {DeviceMessageType} from "@/pages/Device/Locker/History/data";

export async function queryHistoryList(params: {
  deviceNo?: string;
  type?: DeviceMessageType;
  page: number;
  pageSize: number;
}): Promise<{ success: boolean; total: number; pageCount: number; data: MoteInfoItem[] | null }> {
  const {deviceNo, type, page, pageSize} = params;

  const url = type === 'status' ? `get_device_mote_status_list` : `get_device_mote_info_list`;

  const res: ResponsePaginationData<MoteInfoItem> = await request(
      `/device/${url}`,
      {
        params: {page, pageSize, deviceNo},
      },
  );

  if (res?.code !== SUCCESS_CODE) {
    return {success: false, total: 0, pageCount: 0, data: []};
  }

  return {
    success: true,
    total: res.data.total || 0,
    pageCount: res.data.pageSize || 10,
    data: res.data.list.map(item => ({
      ...item,
      createAt: item.createAt ?? '--',
      stateUpdateAt: item.stateUpdateAt ?? '--',
      statusList: DeviceStatusParser.parser(item.statusBox),
    })),
  };
}
